/**
 *******************************************************************************
 * \brief   CAS EBX Project Work "Kaffeemaschine"
 *
 * \file    readFromDevice.c
 * \version 1.0
 * \date    18.08.2010
 * \author  Liechti Beat
 * 
 * \details	reads from device
 * \details build with: 
 * \details gcc readFromDevice.c -o readFromDevice
 * \details then run with (example):
 * \details readFromDevice /dev/c2service
 *
 * \remark  Last Modifications:
 * \remark  V1.0, 18.08.2010   Initial release
 *******************************************************************************
 **/

#include <errno.h>
#include <termios.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <poll.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/wait.h>
#include <time.h>
#include <sys/times.h>
#include <sched.h>

#define RETCODE_SUCCESS		0
#define RETCODE_ERROR		-1

void usage(char *progname)
{
	printf("usage:\n\t%s /dev/<c2device>\n", progname);
}

int main(int argc, char** argv)
{
	int retCode = -RETCODE_ERROR;
	if (argc == 2)
	{
		// -- expecting a string argv[1]
		char *devicePath = argv[1];
		int fd = open(devicePath, O_RDONLY); // | O_NONBLOCK);
		if (fd >= 0)
		{
			int value;
			ssize_t size = read(fd, &value, sizeof(value));
			if (size != sizeof(value))
			{
				printf("failed to read value, got return code %ld\n", (long)size);
				retCode = EFAULT;
			}
			else
			{
				printf("sucessfully read value: %d\n", value);
				retCode = RETCODE_SUCCESS;
			}
			close(fd);
		}
		else
		{
			printf("failed to open file, got return code %d\n", fd);
		}
	}
	else
	{
		usage(argv[0]);
	}
	return retCode;
}
